package com.hotel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotel.model.auto.User;
import com.hotel.service.IUserService;
import com.hotel.untils.SMS.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author
 * @since
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private IUserService iUserService;

    /*查询用户*/
    @GetMapping("/getUser")
    public List<User> getUser() {
        return iUserService.list();
    }

    /*通过id查询用户*/
    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable("id") int id) {
        return iUserService.getById(id);
    }

    /*修改用户*/
    @PostMapping("/updateUser")
    public void updateUser(@RequestBody User user) {
        System.out.println(user);
        iUserService.updateById(user);
    }

    /*添加用户*/
    @PostMapping("/setUser")
    public void setUser(@RequestBody User user) {
        System.out.println(user);
        iUserService.save(user);
    }

    /*删除用户*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id) {
        return iUserService.removeById(id);
    }

    //点击发送验证码
    @PostMapping("/sendMs/{phone}")
    public String sendMs(HttpServletRequest request, @PathVariable("phone") String phoneNumber) {
        if (phoneNumber != null && !phoneNumber.equals("")) {
            String s = SMSUtil.sendSMS(request, phoneNumber);
            request.getSession().setAttribute("Code", s);
            System.out.println(s);
            return s;
        } else {
            return "error";
        }
    }

    /*前台用户登录*/
    @PostMapping("/login")
    public int Login(@RequestBody User user) {
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        User login = iUserService.login(user.getUsername(), user.getPassword());
        System.out.println(login.getId());
        if (login != null) {
            int id = login.getId();
            int a = Math.toIntExact(id);
            return a;
        } else {
            return -1;
        }
    }

    /*后台登录*/
    @PostMapping("/admin")
    public int selectAdmin(@RequestBody User user) {
        System.out.println(user);
        int i = 0;
        String username = user.getUsername();
        System.out.println(username);
        try {
            i = iUserService.selectAdmin(username);
            System.out.println(i);
        } catch (Exception e) {
            return -1;
        }
        if (i == 1) {
            User login = iUserService.login(user.getUsername(), user.getPassword());
            if (login != null) {
                int id = login.getId();
                int a = Math.toIntExact(id);
                return a;
            } else {
                return -1;
            }
        } else {
            return -1;
        }
    }

    /*模糊查询用户数据*/
    @PostMapping("/selectUser/{input}")
    public List<Map<String, Object>> selectUser(@PathVariable("input") String value) {
        System.out.println(value);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        /*核心模糊算法*/
        queryWrapper.like("username", value);
        List<Map<String, Object>> maps = iUserService.listMaps(queryWrapper);
        return maps;
    }

    /*批量删除用户*/
    @PostMapping("/deletedAllById")
    public boolean deletedAllById(@RequestBody int[] delarr) {
        for (int i : delarr) {
            System.out.println(i);
            iUserService.removeById(i);
        }
        return true;
    }

}
